using NewFormatProject.Infrastructure.EFCore;
using NewFormatProject.Infrastructure.Dto.Responses.Objects;
using Microsoft.EntityFrameworkCore;
using System.Linq;
namespace NewFormatProject.Server.Instances.Extends.Entitys
{
    public static class SysUsersExtend
    {
        public static ResponseSysUsersDto ToResponseDto(this SysUsers entity,DbContext dbContext)
        {
            string jobName = dbContext.Set<Jobs>().Where(d => d.JId == entity.UJobId).Select(d => d.JName).SingleOrDefault();
            int? departmentID = dbContext.Set<Jobs>().Where(d => d.JId == entity.UJobId).SingleOrDefault()?.JDepartmentId;
            string departmentName = dbContext.Set<SysDepartments>().Where(d => d.DId == departmentID).Select(d => d.DName).SingleOrDefault();
            return new ResponseSysUsersDto
            {
                id = entity.UId,
                name = entity.UName,
                account = entity.UAccount,
                role = entity.URole,
                avatar = entity.UAvatar,
                phone = entity.UPhone,
                jobId = entity.UJobId,
                jobName=jobName,
                departmentId=departmentID ,
                departmentName=departmentName,
                isActive = entity.UIsActive,
                isBan = entity.UIsBan,
                createTime = entity.UCreateTime,
            };
        }
    }
}
